var app = getApp();
Page({
  data: {
    imageUrlFix: app.d.imageUrlFix,
    imgUrls: [],
    imgUrl: [],
    categoryComList: [],
    imgUrlsStatus: false,
    imgUrlStatus: false,
    categoryComStatus: false,
    homeTopImg: [],
    homeTopImgStatus: false,
    introduce: [],
    introduceStatus: false,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: true,
    productData: [],
    productDataStatus: false,
    jdtjListT: [],
    jdtjListRt: [],
    jdtjListRb: [],
    buttomGoods: [],
    proCat:[],
    page: 2,
    index: 2,
    brand:[],
    // 滑动
    imgUrl: [],
    kbs:[],
    lastcat:[],
    course:[],
    //配送小贴士的内容
    inArea: '福州市区内',
    outArea: '福州市区外',
    inAreaContent: '当日19:00前订购，即日配送(满100元起送，运费10元/kg)',
    outAreaContent: '48小时内发货(运费12元/kg)',
    queryPrioritys: {},
    rushBuyFoods: [],//限量抢购商品
    introduceFoods: [],//推荐食材
  },
//跳转到搜索页面
  toSearch:function(){
    wx.navigateTo({
      url: '../indexsearch/indexsearch',
    })
  },

//跳转商品列表页   
listdetail:function(e){
    wx.navigateTo({
      url: '../listdetail/listdetail?title='+e.currentTarget.dataset.title,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
//点击加载更多
getMore:function(e){
  wx.switchTab({
    url: '../category/index'
  })
},

toCategory: function(e){
  var currtype = parseInt(e.currentTarget.dataset.currtype);
  app.d.currType = currtype;
  wx.switchTab({
    url: '../category/index',
  })
},

/**
 * 下拉刷新事件
 */
onPullDownRefresh: function (){
  var that = this;
  app.d.indexCallback("refresh");
},

  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },

  onLoad: function (options) {
    wx.showNavigationBarLoading();
    var date=new Date();
    //获取分享参数中的分销商id
    if (options.duid) {
      app.d.duserId = options.duid;
    }

    //获取链接中的分享ID
    if (options.shareId){
      app.d.shareId = options.shareId;
    }

    var that = this;
    app.d.indexCallback=function(callType){
      wx.showNavigationBarLoading();
      var onLoadNum = that.data.queryPrioritys;
      onLoadNum["onLoadNum"] = 0;
    // wx.showNavigationBarLoading();
    // wx.setNavigationBarTitle({
    //   title:"大肚商城首页"
    // });
    //推荐商品列表
    // app.request("IndexService", "queryPriority", {
    //   seatCode: "main_product_v2",
    //   seatType: 3
    // }, function (data) {
    //   onLoadNum["onLoadNum"] += 1;
    //   if (onLoadNum["onLoadNum"] >= 6) {
    //     wx.hideNavigationBarLoading();
    //     wx.stopPullDownRefresh();
    //   }
    //   if(data.priorityList != null && data.priorityList.length > 0){
    //     that.setData({
    //       productData: data.priorityList,
    //       queryPrioritys: onLoadNum,
    //       productDataStatus: true
    //     });
    //   }else{
    //     that.setData({
    //       productDataStatus:false
    //     })
    //   }
    // });

    //限量抢购商品
    app.request("IndexService", "queryPriority", {
      seatCode: "rush_buy_food_v2",
      seatType: 2,
    }, function(data){
      onLoadNum["onLoadNum"] += 1;
      if (onLoadNum["onLoadNum"] >= 6) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      var rushBuyFoodList = [];
      if (data.priorityList != null && data.priorityList.length > 0){
        for (var i = 0; i < data.priorityList.length; i++){
          var item = {};
          if(i%2 == 0){
            item["left"] = data.priorityList[i];
            if (data.priorityList[i+1] != undefined){
              item["right"] = data.priorityList[i + 1];
            }
            i++;
          }    
          rushBuyFoodList.push(item);
        }
      }
      that.setData({
        rushBuyFoods: rushBuyFoodList,
      });
    })

    // 推荐食材
    app.request("IndexService", "queryPriority", {
      seatCode: "main_introduce_food_v2",
      seatType: 2,
    }, function(data){
      onLoadNum["onLoadNum"] += 1;
      if (onLoadNum["onLoadNum"] >= 6) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      var introduceFoodList = [];
      if (data.priorityList != null && data.priorityList.length > 0) {
        for (var i = 0; i < data.priorityList.length; i++) {
          var item = {};
          if (i % 2 == 0) {
            item["left"] = data.priorityList[i];
            if (data.priorityList[i + 1] != undefined) {
              item["right"] = data.priorityList[i + 1];
            }
            i++;
          }
          introduceFoodList.push(item);
        }
        that.setData({
          productDataStatus: true
        })
      }else{
        that.setData({
          productDataStatus: false
        })
      }
      that.setData({
        introduceFoods: introduceFoodList,
      })
    });
    
    //顶部静态图片
    app.request("IndexService", "queryPriority", {
      seatCode: "home_top_img_v2",
      seatType: 2
    }, function (data) {
      onLoadNum["onLoadNum"] += 1;
      if (onLoadNum["onLoadNum"] >= 6) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      if (data.priorityList != null && data.priorityList.length > 0){
        var homeTopImg = data.priorityList[0];
        that.setData({
          homeTopImg: homeTopImg,
          queryPrioritys: onLoadNum,
          homeTopImgStatus: true,
        });
      }else{
        that.setData({
          homeTopImgStatus: false,
        })
      } 
    });
    //店铺介绍
    app.request("IndexService", "queryPriority", {
      seatCode: "main_introduce_v2",
      seatType: 2
    }, function (data) {
      onLoadNum["onLoadNum"] += 1;
      if (onLoadNum["onLoadNum"] >= 6) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      if(data.priorityList != null && data.priorityList.length > 0){
        var introduce = data.priorityList[0];
        that.setData({
          introduce: introduce,
          queryPrioritys: onLoadNum,
          introduceStatus: true
        });
      }else {
        that.setData({
          introduceStatus: false
        });
      }
      
    });
    //轮播图1
    app.request("IndexService","queryPriority",{
      seatCode: "main_top-1_v2",
      seatType: 1
    },function(data){
      onLoadNum["onLoadNum"] += 1;
      if (onLoadNum["onLoadNum"] >= 6) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      if (data.priorityList != null && data.priorityList.length > 0){
        var imgUrls = data.priorityList;
        that.setData({
          imgUrls: imgUrls,
          queryPrioritys: onLoadNum,
          imgUrlsStatus: true,
        });
      }else {
        that.setData({
          imgUrlsStatus: false,
        });
      }
    });

    //轮播图2
    app.request("IndexService", "queryPriority", {
      seatCode: "main_top-2_v2",
      seatType: 1
    }, function (data) {
      onLoadNum["onLoadNum"] += 1;
      if (onLoadNum["onLoadNum"] >= 6) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      if (data.priorityList != null && data.priorityList.length > 0) {
        var imgUrl = data.priorityList;
        that.setData({
          imgUrl: imgUrl,
          queryPrioritys: onLoadNum,
          imgUrlStatus: true,
        });
      } else {
        that.setData({
          imgUrlStatus: false,
        });
      }
    });

    //分类推荐
    app.request("IndexService", "queryCategoryPriority", {
      seatCode: "home_category_img_v2",
      seatType: 2
    }, function (data) {
      onLoadNum["onLoadNum"] += 1;
      if (onLoadNum["onLoadNum"] >= 6) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      if (data.categoryPriority != null && data.categoryPriority.length > 0) {
        var categoryComList = data.categoryPriority;
        console.log(categoryComList);
        that.setData({
          categoryComList: categoryComList,
          queryPrioritys: onLoadNum,
          categoryComStatus: true,
        });
      } else {
        that.setData({
          categoryComStatus: false,
        });
      }
    });
    };
    app.getUserInfo();
    
  },
  onShareAppMessage: function () {
    var data = new Date();
    var shared = data.getTime()+"";
    var shareId = shared.substring(8, shared.length) + app.d.userId;
    return {
      title: '大肚商城',
      path: '/pages/index/index?duid=' + app.d.userId + "&shareId=" + shareId,
      success: function(res) {
        app.request("DistributorService", "shareLog", {
          uid: app.d.userId,
          shareId: shareId,
        }, function(data){
          // 分享成功
          console.log(res);
        })
      },
      fail: function(res) {
        // 分享失败
        console.log(res);
      }
    }
  }
});